import { loginApi, getUserApi } from '@/api/user.js'
import { setToken, getToken, delToken } from '@/utils/auth'
// 用户模块：存储token、个人信息
export default {
  namespaced: true,
  // 定义变量
  state: {
    token: getToken() || '',
    userInfo: {
    }
  },
  // 修改变量
  mutations: {
    getToken (state, payload) {
      state.token = payload
      setToken(payload)
    },
    delToken (state) {
      state.token = ''
      delToken()
    },
    setUserInfo (state, payload) {
      state.userInfo = payload
    }
  },
  // 发请求
  actions: {
    async getTokenAction (ctx, payload) {
      const { data } = await loginApi(payload)
      // console.log(data)
      if (data.code === 0) {
        ctx.commit('getToken', data.token)
      }
      return data
    },
    async getUserAction (ctx) {
      const { data } = await getUserApi()
      console.log('用户信息', data)
      ctx.commit('setUserInfo', data.data)
    }
  }
}
